package com.example.studentcourse.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.studentcourse.common.Result;
import com.example.studentcourse.entity.Student;
import com.example.studentcourse.service.IStudentService;
import com.example.studentcourse.vo.StudentQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学生管理接口
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private IStudentService studentService;

    /**
     * 分页条件查询学生列表
     * @param query 查询参数
     * @return 分页结果
     */
    @PostMapping("/list")
    public Result<Page<Student>> list(@RequestBody StudentQueryVO query) {
        // 构建查询条件
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        // 姓名模糊查询
        wrapper.like(StringUtils.hasText(query.getName()), Student::getName, query.getName())
                // 学号模糊查询
                .like(StringUtils.hasText(query.getStudentNo()), Student::getStudentNo, query.getStudentNo())
                // 年级模糊查询
                .like(StringUtils.hasText(query.getGrade()), Student::getGrade, query.getGrade())
                // 性别精确查询
                .eq(query.getGender() != null, Student::getGender, query.getGender());

        // 执行分页查询
        Page<Student> page = studentService.page(new Page<>(query.getCurrent(), query.getSize()), wrapper);
        return Result.success(page);
    }

    /**
     * 新增学生
     */
    @PostMapping
    public Result<?> save(@RequestBody Student student) {
        studentService.save(student);
        return Result.success(null);
    }

    /**
     * 修改学生信息
     */
    @PutMapping
    public Result<?> update(@RequestBody Student student) {
        studentService.updateById(student);
        return Result.success(null);
    }

    /**
     * 删除单个学生
     */
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        studentService.removeById(id);
        return Result.success(null);
    }

    /**
     * 批量删除学生
     */
    @DeleteMapping("/batch")
    public Result<?> deleteBatch(@RequestBody List<Long> ids) {
        studentService.removeByIds(ids);
        return Result.success(null);
    }
} 